using System;
using System.Data;
using System.Data.SqlClient;

namespace MessageBoard
{
	/// <summary>
	/// Summary description for Translation.
	/// </summary>
	public class Translation : Connection
	{
		private CustomControl _CustomControl;
		private string _Word;
		private Language _Language;
		private string _Equal;


     	public Translation()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		public static Translation Get(System.Data.DataRow dr)
		{
			Translation translation = new Translation();
			translation.CustomControl = CustomControl.Get(int.Parse(dr["CustomControlId"].ToString()));
			translation.Word = dr["Word"].ToString();
			translation.Language = Language.Get(int.Parse(dr["LanguageId"].ToString()));
			translation.Equal = dr["Equal"].ToString();
			return translation;
		}

		public static Translation Get(CustomControl customControl, string word, Language language)
		{
			DataSet ds = new DataSet();
			SqlDataAdapter getData = new SqlDataAdapter("SELECT * FROM " + Connection.tblTranslation + " WHERE [CustomControlId] = @CustomControlId AND [Word] = @Word AND [LanguageId] = @LanguageId", DbConnection);
			getData.SelectCommand.Parameters.Add("@CustomControlId", customControl.Id);
			getData.SelectCommand.Parameters.Add("@Word", word);
			getData.SelectCommand.Parameters.Add("@LanguageId", language.Id);
			getData.Fill(ds);
			if(ds.Tables[0].Rows.Count>0)
			{
				return Get(ds.Tables[0].Rows[0]);
			}
			else
			{
				Translation translation = new Translation();
				translation.CustomControl = customControl;
				translation.Word = word;
				translation.Language = language;
				translation.Equal = "-*-";
				translation.Insert();
				return translation;
			}
		}

		public void Insert()
		{
			DataSet ds = new DataSet();
			SqlDataAdapter getData = new SqlDataAdapter(@"
			INSERT INTO " + Connection.tblTranslation + @" 
			([CustomControlId], [Word], [LanguageId], [Equal])
			VALUES
			(@CustomControlId, @Word, @LanguageId, @Equal);
			SELECT @@identity as i", DbConnection);
			getData.SelectCommand.Parameters.Add("@CustomControlId", CustomControl.Id);
			getData.SelectCommand.Parameters.Add("@Word", Word);
			getData.SelectCommand.Parameters.Add("@LanguageId", Language.Id);
			getData.SelectCommand.Parameters.Add("@Equal", Equal);
			getData.Fill(ds);
		}
				
		#region Properties
		/// <summary>
		/// The CustomControl.
		/// </summary>
		public CustomControl CustomControl
		{
			get
			{
				return _CustomControl;
			}
			set
			{
				_CustomControl = value;
			}
		}

		/// <summary>
		/// The Word.
		/// </summary>
		public string Word
		{
			get
			{
				return _Word;
			}
			set
			{
				_Word = value;
			}
		}

		/// <summary>
		/// The Translation.
		/// </summary>
		public Language Language
		{
			get
			{
				return _Language;
			}
			set
			{
				_Language = value;
			}
		}

		/// <summary>
		/// The Translation.
		/// </summary>
		public string Equal
		{
			get
			{
				return _Equal;
			}
			set
			{
				_Equal = value;
			}
		}

		
		#endregion
	}
}
